<template>
  <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" size="large">
    <el-form-item prop="username">
      <el-input v-model="loginForm.mobile" placeholder="请输入手机号">
        <template #prefix>
          <el-icon class="el-input__icon">
            <user />
          </el-icon>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item prop="code">
      <el-input v-model="loginForm.code" placeholder="输入验证码">
        <template #prefix>
          <el-icon class="el-input__icon">
            <user />
          </el-icon>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input v-model="loginForm.password" type="password" placeholder="输入新密码：6-16位字符，包含数字和字符">
        <template #prefix>
          <el-icon class="el-input__icon">
            <user />
          </el-icon>
        </template>
      </el-input>
    </el-form-item>
  </el-form>

  <div class="login-btn">
    <el-button :icon="UserFilled" round size="large" type="primary" :loading="loading" @click="resetPassword(loginFormRef)">
      登录
    </el-button>
    <el-button icon="Refresh" round size="large" type="" @click="backToLogin">返回</el-button>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
// import { useRouter } from "vue-router";
import { Login } from '@/api/interface'

// import { HOME_URL } from "@/config";
// import { getTimeState } from "@/utils";

// import { ElNotification } from "element-plus";
// import { loginApi } from "@/api/modules/login";
// import { useUserStore } from "@/stores/modules/user";
// import { useTabsStore } from "@/stores/modules/tabs";
// import { useKeepAliveStore } from "@/stores/modules/keepAlive";
// import { initDynamicRouter } from "@/routers/modules/dynamicRouter";
import { UserFilled } from '@element-plus/icons-vue'
import type { ElForm } from 'element-plus'
// import md5 from "md5";

const $emit = defineEmits(['toggleFormView'])

// const router = useRouter();
// const userStore = useUserStore();
// const tabsStore = useTabsStore();
// const keepAliveStore = useKeepAliveStore();

type FormInstance = InstanceType<typeof ElForm>
const loginFormRef = ref<FormInstance>()
const loginRules = reactive({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
})

const loading = ref(false)
const loginForm = reactive<Login.ResetpasswordForm>({
  mobile: '',
  password: '',
  code: ''
})

const resetPassword = (formEl: FormInstance | undefined) => {
  if (!formEl) return

  formEl.validate(async valid => {
    if (!valid) return
  })
}

const backToLogin = () => {
  $emit('toggleFormView')
}

onMounted(() => {
  // 监听 enter 事件（调用登录）
  document.onkeydown = (e: KeyboardEvent) => {
    e = (window.event as KeyboardEvent) || e
    if (e.code === 'Enter' || e.code === 'enter' || e.code === 'NumpadEnter') {
      if (loading.value) return
      // login(loginFormRef.value);
    }
  }
})
</script>

<style scoped lang="scss">
@import '../index.scss';
</style>
